package cn.tianya.irock.data.model;

import java.sql.Date;


import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import cn.tianya.irock.enums.ApplicationExceptionType;
import cn.tianya.irock.enums.SystemExceptionType;
import cn.tianya.irock.exceptions.SystemException;

public class UserEx {
	private JSONObject json;
	
	private UserEx(JSONObject obj) {
		json = obj;
	}
	
	private Object get(String key) {
		try {
			return json.get(key);
		} catch (JSONException e) {
			return null;
		}
	}
	
	public static UserEx parse(String str) {
		JSONObject json = null;
		UserEx user = null;
		
		try {
			json = new JSONObject(str);
			user = new UserEx((JSONObject)json.get("user"));
		} catch (JSONException ex) {
			throw new SystemException(SystemExceptionType.APIError, "ICE JSON ERROR");
		}
		return user;
	}
	
	public String getUsername() {
		return (String)get("user_name");
	}
	
	public int getLoginCount() {
		return (Integer)get("login_counter");
	}
	
	public Date getRegisterDate() {
		long reg_date = (Long)get("register_date");
		return new Date(reg_date);
	}
}
